//
//  IMTList.m
//  LinePhoto
//
//  Created by Kyle on 14-4-13.
//  Copyright (c) 2014年 孙凯旋. All rights reserved.
//

#import "IMTList.h"

@implementation IMTList

- (id)init {
    self = [super init];
    if (self) {
        self.isReload = YES;
        self.pageIndex = 1;
    }
    return self;
}

- (NSMutableArray *)dataList {
    if (!_dataList) {
        _dataList = [[NSMutableArray alloc] init];
    }
    return _dataList;
}

- (NSMutableArray *)viewModelList {
    if (!_viewModelList) {
        _viewModelList = [[NSMutableArray alloc] init];
    }
    return _viewModelList;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
    self = [super init];
    if (self) {
        self.dataList = [aDecoder decodeObjectForKey:@"dataList"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder {
    [aCoder encodeObject:self.dataList forKey:@"dataList"];
}

//通过请求加载数据
- (void)loadDataFromResponseData:(id)responseData andRequestType:(RequestType)requestType {
    NSDictionary *data = nil;
    if (requestType == RequestTypeGetPostDetail || requestType == RequestTypeGetTaskDetail || requestType == RequestTypeGetTaskList) {
        data = responseData;
    } else {
        data = [responseData objectForKey:@"data"];
    }
        self.pageIndex = [self stringFromResponseData:[data objectForKey:@"pageIndex"]].intValue;
        self.pageSize = [self stringFromResponseData:[data objectForKey:@"pageSize"]].intValue;
        self.totalCount = [self stringFromResponseData:[data objectForKey:@"totalCount"]].intValue;
        self.totalPages = [self stringFromResponseData:[data objectForKey:@"totalPages"]].intValue;
    
    if (self.isReload) {
        if ([self.dataList count]) {
            [self.dataList removeAllObjects];
            [self.viewModelList removeAllObjects];
        }
        self.isReload = NO;
    }
    
    if (self.pageIndex >= self.totalPages) {
        self.loadMore = NO;
    } else {
        self.loadMore = YES;
        self.pageIndex++;
    }
}

//返回计数
- (NSInteger)count {
    return [self.dataList count];
}

//返回对象
- (id)objectAtIndex:(NSInteger)index {
    return [self.dataList objectAtIndex:index];
}

//移除所有对象
- (void)removeAllObjects {
    [self.dataList removeAllObjects];
}

//移除对象
- (void)removeObjectAtIndex:(NSInteger)index {
    [[self mutableArrayValueForKey:@"dataList"] removeObjectAtIndex:index];
}

- (void)addObject:(id)object {
    [[self mutableArrayValueForKey:@"dataList"] addObject:object];
}

- (void)insertObject:(id)object atIndex:(NSUInteger)index {
    [[self mutableArrayValueForKey:@"dataList"] insertObject:object atIndex:index];
}

- (void)saveArchiver {
}

- (void)loadArchiver {

}

- (void)loadPageInfoWithData:(id)data {
    self.pageIndex = [self stringFromResponseData:[data objectForKey:@"pageIndex"]].intValue;
    self.pageSize = [self stringFromResponseData:[data objectForKey:@"pageSize"]].intValue;
    self.totalCount = [self stringFromResponseData:[data objectForKey:@"totalCount"]].intValue;
    self.totalPages = [self stringFromResponseData:[data objectForKey:@"totalPages"]].intValue;
    
    if (self.pageIndex >= self.totalPages) {
        self.loadMore = NO;
    } else {
        self.loadMore = YES;
        self.pageIndex++;
    }
}

@end
